$(function () {
	//表单颜色
	showtable('orderList', 'tgray', '');

	//订单课程总价
	var orderPrice = $('#order_total_price').attr('price');
	//帐户余额
	var balance = $('#balance').text();
	if (Number(balance) < Number(orderPrice)){
		$('#balance_p').show();
		$('#balance_btn').hide();
		$('#payment_type').show();
	}else{
		$('#balance_p').hide();
		$('#balance_btn').show();
		$('#payment_type').hide();
	}

	//用户优惠卷列表
	$(".orderbox_list select").each(function(){
		var cid = $(this).attr("cid");
		var sel_id = $(this).attr("id");
		getCounpon(cid, sel_id);
	});

	$('select[name="data[coupon][]"]').change(function(){
		//默认优惠卷总额为0
		var couponTotalPrice = 0;
		//课程优惠价
		var coursePrice = $(this).attr('price');
		//使用的优惠卷价格
		var couponPrice = $(this).find('option:selected').text();
		//fixed by liushuai
			var priceArr = couponPrice.split("元");
			couponPrice = parseInt(priceArr[0]);
		//end fixed;
		if(isNaN(couponPrice)) couponPrice=0;
		//当前课程价格
		var curprice = 0;
		if (parseInt(coursePrice) > parseInt(couponPrice)) {
			curprice = coursePrice-couponPrice;
		}
		var selcouponprice = $(this).attr('id')+'_price';
		$('#'+selcouponprice).html(curprice);

		//循环获取优惠卷
		$.each($('select[name="data[coupon][]"]'), function() {
			var couponPrice = $(this).find('option:selected').text();
			//fixed by liushuai
			var pArr = couponPrice.split("元");
				couponPrice = parseInt(pArr[0]);
			//end fixed
			var curCoursePrice = $(this).attr('price');
			if(isNaN(couponPrice))couponPrice=0;
			if(parseInt(curCoursePrice) > parseInt(couponPrice)) {
				couponTotalPrice += Number(couponPrice);
			} else {
				couponTotalPrice += Number(curCoursePrice);
			}
		})
		var orderTotalPrice = orderPrice-couponTotalPrice;
		$('#coupon_total_price').html(couponTotalPrice);
		$('#order_total_price').html(orderTotalPrice);

		if (Number(balance) < orderTotalPrice){
			$('#balance_p').show();
			$('#balance_btn').hide();
			$('#payment_type').show();
			var paymentPrice = orderTotalPrice-Number(balance);
			$('#balance_payment').html(Math.ceil(paymentPrice) + '.00');
		}else{
			$('#balance_p').hide();
			$('#balance_btn').show();
			$('#payment_type').hide();
		}
		checkPayment();
	});

	//省市区联动
	renderAreaSelect();

	//邮编获取
	$("#register_city, #register_country").change(function(){
		var country_id = $("#register_country").val();
		zipSelect(country_id);
	});

	//选择收货地址
	$('.d2 input').live('click',function(){
		if(this.checked){
			$('.d2_con').hide();
			$(this).parent('.d2').siblings('.d2_con').removeClass('active');
			$(this).parent('.d2').next('.d2_con').addClass('active').show();
			$(this).parent('.d2').addClass('active').siblings('.d2').removeClass('active');
			checkPayment();
		}
	});

	$("#isInvoice").click(function(){
		checkPayment();
	});

	//提交表单的验证
	var _flag = 1;
	$("#btn2").click(function(){
		_flag = 2;
	});
	$("#confirmForm").submit(function(){
		if (($('.orderbox_payment_head:visible .tabs').index($('.orderbox_payment_head .active')[0]) == 1 || $("#isInvoice").attr('checked') == true ) && $("#other_add").attr('checked') == true) {
			if ($.trim($("#name").val()) == ''){
				alert('收货人姓名不能为空');
		        $('#name').focus();
				return false;
			}
			if ($.trim($("#phone").val()) == ''){
				alert('电话/手机不能为空');
		        $('#phone').focus();
				return false;
			}
			if ($.trim($("#register_province").val()) == ''){
				alert('省份不能为空');
		        $('#register_province').focus();
				return false;
			}
			if ($.trim($("#address").val()) == ''){
				alert('收货人详细地址不能为空');
		        $('#address').focus();
				return false;
			}
		}
		if (_flag == 2 && $('.orderbox_payment_head .tabs').index($('.orderbox_payment_head .active')[0]) == 0) {
			$(this).attr('target','_blank');
			showDialog('dialog', 500, 220, '', '');
			return true;
		}
	});
	if($('#pay_address').is(':visible')){
		$('#pay_address').click();
	}
})

// 弹出详细页面
function showDialog(div, width, height, title) {
	$('#' + div).dialog({
		draggable: true,
		resizable: false,
		closeOnEscape: false,
		bgiframe: true,
		height: height,
		width: width,
		title: title,
		modal: true,
		position: ['top', 'center'],
		beforeClose: function() {
			window.location.href = '/shoppingcart/payRedirect/';
		}
	});
};

/* 检测付款方式
 * 
 */
function checkPayment(){
	var _index = $('.orderbox_payment_head:visible .tabs').index($('.orderbox_payment_head .active')[0])
		_isCheck=$('#isInvoice').attr('checked');
		if(_isCheck && _index!=1) {
			$("#invoicehead_div").show();
			addrBoxToggle(1);
		}else if (_isCheck && _index==1){
			$("#invoicehead_div,#invoicehead").show();
			addrBoxToggle(2);
		}else if(!_isCheck && _index!=1){
			$("#cod_delivery_div,#delivery,#delivery_div,#invoicehead_div").hide();
		}else if(!_isCheck &&_index==1){
			$("#invoicehead_div").hide();
			addrBoxToggle(2);
		}else if(_isCheck){
			$("#invoicehead_div").show();
			$("#cod_delivery_div,#delivery,#delivery_div").hide();
		}else{
			$("#cod_delivery_div,#delivery,#delivery_div,#invoicehead_div").hide();
		}
		var _i=(_index==-1)?0:_index;
		$('.orderbox_form_temp').val(_i);
}
/* 切换地址栏
 * 
 */
function addrBoxToggle(n){
	var _addrbox=$("#delivery");				//地址框
	var _addrWrapCash=$("#cod_delivery_div");	//货到付款处存放地址栏的容器
	var _addrWrapInvoice=$("#delivery_div");	//开发票处存放地址栏的容器
	var _offset,_addrboxLeft,_addrboxTop;
	var _addrboxWidth = _addrbox.width();
	var _addrboxHeight = _addrbox.height();

	// 切换到发票处
	if (n == 1) {
		_addrWrapCash.hide();
		_addrWrapInvoice.show(10, function(){
			$(this).css({
				width: _addrboxWidth,
				height: _addrboxHeight
			})
			_offset =$("#invoice_p").offset();
			_addrBoxPosition();
		});
	}
	// 切换到货到付款
	if (n == 2) {
		_addrWrapInvoice.hide();
		_addrbox.show();
		
		_addrWrapCash.show(10,function(){
			$(this).css({
				width: _addrboxWidth,
				height: _addrboxHeight
			});
			_offset = $(".orderbox_payment_dl").offset();
			_addrBoxPosition();;
		})
	}
	// 定位地址框
	function _addrBoxPosition(){
		_addrbox.show().css({
			top: _offset.top + 32,
			left: _offset.left,
			zIndex: '1000'
		});		
	}
}